package top.canyue.eca_manager.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.canyue.eca_manager.bean.Result;
import top.canyue.eca_manager.bean.Todo;
import top.canyue.eca_manager.mapper.TodoMapper;
import top.canyue.eca_manager.service.TodoService;

import java.sql.Date;
import java.util.List;

@Service
public class TodoServiceImpl implements TodoService {
    @Autowired
    private TodoMapper todoMapper;

    @Override
    public Result addEvent(String userId, String event, long endDate) {
        Result result = new Result();
        if (userId.isEmpty()) {
            result.setStatus(500);
            result.setMsg("用户ID不得为空");
            return result;
        }
        if (event.isEmpty()) {
            result.setStatus(500);
            result.setMsg("事件不得为空");
            return result;
        }

        //怕Web端超太多，不好看！
        if (event.length() > 15) {
            result.setStatus(500);
            result.setMsg("代办事件不得大于15个字符！");
            return result;
        }

        // 原则问题，魔法数字要标注
        long ONE_DAY = 86400000;   //endDate传过来的时候是当天的00:00:00
        endDate = endDate + ONE_DAY - 1;   //逻辑上，代办到这天，应该是到这天的最后时刻结束，所以减掉1毫秒
        // 这里留意下 currentTimeMillis() 返回的粒度可能会受操作系统影响
        if (endDate < System.currentTimeMillis()) {
            result.setStatus(500);
            result.setMsg("你必须选择一个未来的时间！");
            return result;
        }


        boolean flag = todoMapper.addEvent(userId, event, new Date(endDate)) > 0;
        if (flag) {
            result.setStatus(200);
            result.setMsg("OK");
            return result;
        }
        result.setStatus(500);
        result.setMsg("添加失败！请检查传入参数。");

        return result;
    }

    @Override
    public Result findById(String id) {
        Result result = new Result();
        if (id.isEmpty()) {
            result.setStatus(500);
            result.setMsg("ID不得为空");
            return result;
        }

        Todo todo = todoMapper.findById(id);
        if (todo == null) {
            result.setStatus(500);
            result.setMsg("找不到对象");
            return result;
        }

        result.setStatus(200);
        result.setMsg("OK");
        result.setData(todo);
        return result;
    }

    @Override
    public Result findByUserId(String userId) {
        Result result = new Result();
        if (userId.isEmpty()) {
            result.setStatus(500);
            result.setMsg("用户ID不得为空");
            return result;
        }

        List<Todo> todo = todoMapper.findByUserId(userId);
        if (todo.isEmpty()) {
            result.setStatus(500);
            result.setMsg("找不到对象");
            return result;
        }
        result.setStatus(200);
        result.setMsg("OK");
        result.setData(todo);
        return result;
    }

    @Override
    public Result deleteById(String id) {
        Result result = new Result();
        if (id.isEmpty()) {
            result.setStatus(500);
            result.setMsg("ID不得为空");
            return result;
        }

        boolean flag = todoMapper.deleteById(id) > 0;
        if (flag) {
            result.setStatus(200);
            result.setMsg("OK");
            return result;
        }
        result.setStatus(500);
        result.setMsg("删除失败！");
        return result;
    }

}
